<%@ page pageEncoding="UTF-8"%>
<%@ include file="import.jsp"%>
<%!
	public static Integer VOTE_ENABLE = 1;
	public static Integer VOTE_CANCEL = 2;
	
	public long number(Voter voter) {
		List<Long> list = QBuilder.db("basedb").queryHql("select count(id) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId());
		return list.get(0);
	}
	
	// 检查某人当天是否投个同一个人
	public boolean repeat(VoteInfo vi) {
		List list = QBuilder.db("basedb").queryHql("from VoteInfo where hospital = " + vi.getHospital() + " and status = " + STATUS_ENABLE + " and voterId = " + vi.getVoterId() + " and uid = '" + vi.getUid() + "' and date = '" + vi.getDate() + "' and cancel = " + VOTE_ENABLE);
		return list.size() > 0;
	}
	
	// 检查投票是否在合法时间内
	public boolean checkDate(VoteInfo vi) {
		List list = QBuilder.db("basedb").queryHql("select new map (v.startTime as start, v.endTime as end) from Voter as vr, Vote as v where v.id = vr.voteId and vr.id = " + vi.getVoterId() + " and v.status = " + STATUS_ENABLE + " and vr.status = " + STATUS_ENABLE);
		if (list.size() > 0) {
			Map map = (Map)list.get(0);
			long start = Tools.getLong(map.get("start") + "", 0L);
			long end = Tools.getLong(map.get("end") + "", 0L);
			long now = DateUtil.parseDate(vi.getDate()).getTime();
			if (start != 0 && end != 0)
				return (now >= start && now <= end);
		}
		return true;
	}
	
	// 检查某人当天是否超过10票
	public boolean over(VoteInfo vi, int limit) {
		List list = QBuilder.db("basedb").queryHql("from VoteInfo where hospital = " + vi.getHospital() + " and status = " + STATUS_ENABLE + " and uid = '" + vi.getUid() + "' and date = '" + vi.getDate() + "' and cancel = " + VOTE_ENABLE);
		return list.size() >= limit;
	}
	
	// 投票
	public Message vote(VoteInfo vi, int limit) {
		Message msg = new Message();
		vi.setDate(DateUtil.formatDate(new Date()));
		if (!checkDate(vi)) {
			return new Message(30090, "请明确投票时间");
		}
		if (over(vi, limit)) { // 超过票数
			return new Message(30090, "您今天已经投了" + limit + "票");
		}
		if (repeat(vi)) { // 是否重复投票
			return new Message(30090, "您今天已经投过该人");
		}
		vi.setCancel(VOTE_ENABLE);
		Integer id = createItem(vi);
		msg.put("id", id);
		return msg;
	}
	
	// 创建投票
	public Message createVote(Map<String, String> parameters){
		Message msg = new Message();
		Vote vote = Vote.fromMap(parameters, Vote.class);
		vote.setState(VOTE_ENABLE);
		vote.setStartTime(DateUtil.parseDate(parameters.get("start")).getTime());
		vote.setEndTime(DateUtil.parseDate(parameters.get("end")).getTime());
		Integer id = createItem(vote);
		if (id == null) 
			return new Message(30090, "创建投票失败");
		msg.put("id", id);
		msg.put("title", vote.getTitle());
		return msg;
	}
	
	// 创建被投票人
	public Message createVoter(Voter voter){
		Message msg = new Message();
		voter.setNumber((number(voter) + 1) + "");
		voter.setState(VOTE_ENABLE);
		Integer id = createItem(voter);
		if (id == null) 
			return new Message(30090, "创建失败");
		msg.put("id", id);
		return msg;
	}
	
	// 查询投票
	public Message queryVote(Vote vote) {
		Message msg = new Message();
		List<Vote> list = QBuilder.db("basedb").queryHql("select new map (id as id, title as title, CONCAT(FROM_UNIXTIME(startTime / 1000, '%Y-%m-%d'), '') as start_time, CONCAT(FROM_UNIXTIME(endTime / 1000, '%Y-%m-%d'), '') as end_time, CONCAT(FROM_UNIXTIME(cTime / 1000, '%Y-%m-%d'), '') as create_time) from Vote where hospital = " + vote.getHospital() + " and status = " + STATUS_ENABLE + " order by cTime desc");
		if (list.size() == 0) {
			return new Message(30090, "暂无数据");
		}
		msg.put("votes", list);
		return msg;
	}
	
	// 查询投票结果
	public Message queryResult(Voter voter, VoteInfo vi, Integer count, Integer pageNum) {
		Message msg = new Message();
		vi.setDate(DateUtil.formatDate(new Date()));
		List totalNumber = QBuilder.db("basedb").queryHql("select count(id) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId());
		List list = QBuilder.db("basedb").queryHql("select new map(id as id, expertName as expert_name, number as number, avatar as avatar, title as title, deptName as dept_name, branchName as branch_name) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId(), count, pageNum);
		if (list.size() == 0) {
			return new Message(30090, "暂无数据");
		}
		String _voterIds = "-1";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String voterId = Tools.getDesc(map.get("id"), "");
			if ("".equals(voterId)) 
				continue;
			_voterIds += "," + voterId;
		}
		List counts = QBuilder.db("basedb").queryHql("select new map(voterId as id, count(id) as count) from VoteInfo where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voterId in (" + _voterIds + ") group by voterId"); 
		msg.put("voters", list);
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			map.put("count", 0);
			String voterId = Tools.getDesc(map.get("id"), "");
			for (int j = 0; j < counts.size(); j++) {
				Map temp = (Map)counts.get(j);
				String id = Tools.getDesc(temp.get("id"), "-1");
				if (voterId.equals(id)) {
					map.put("count", Tools.getInt(temp.get("count") + "", 0));
					break;
				}
			}
		}
		List<Integer> voterIds = QBuilder.db("basedb").queryHql("select voterId from VoteInfo where hospital = " + vi.getHospital() + " and status = " + STATUS_ENABLE + " and uid = '" + vi.getUid() + "' and date = '" + vi.getDate() + "' and cancel = " + VOTE_ENABLE);
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			int voterId = Tools.getInt(map.get("id") + "", 0);
			map.put("state", voterIds.contains(voterId));
		}
		pageNum = Tools.getInt(totalNumber.get(0) + "", 1);
		msg.put("total_number", (pageNum % count) == 0 ? pageNum / count : pageNum / count + 1);
		return msg;
	}
	
	// 查询投票结果
	public Message queryResultOrder(Voter voter, Integer count, Integer pageNum) {
		Message msg = new Message();
		List totalNumber = QBuilder.db("basedb").queryHql("select count(id) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId());
		List list = QBuilder.db("basedb").queryHql("select new map(id as id, expertName as expert_name, number as number, avatar as avatar, title as title, deptName as dept_name, branchName as branch_name) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId(), count, pageNum);
		if (list.size() == 0) {
			return new Message(30090, "暂无数据");
		}
		String _voterIds = "-1";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String voterId = Tools.getDesc(map.get("id"), "");
			if ("".equals(voterId)) 
				continue;
			_voterIds += "," + voterId;
		}
		List counts = QBuilder.db("basedb").queryHql("select new map(voterId as id, count(id) as count) from VoteInfo where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voterId in (" + _voterIds + ") group by voterId"); 
		msg.put("voters", list);
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			map.put("count", 0);
			String voterId = Tools.getDesc(map.get("id"), "");
			for (int j = 0; j < counts.size(); j++) {
				Map temp = (Map)counts.get(j);
				String id = Tools.getDesc(temp.get("id"), "-1");
				if (voterId.equals(id)) {
					map.put("count", Tools.getInt(temp.get("count") + "", 0));
					break;
				}
			}
		}
		// 按票数进行排序
		Collections.sort(list, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				return Tools.getInt(((Map)o2).get("count") + "", 0) - Tools.getInt(((Map)o1).get("count") + "", 0);
			}
		});
		pageNum = Tools.getInt(totalNumber.get(0) + "", 1);
		msg.put("total_number", (pageNum % count) == 0 ? pageNum / count : pageNum / count + 1);
		return msg;
	}
	
	// 统计
	public Message count(Voter voter) {
		Message msg = new Message();
		List list = QBuilder.db("basedb").queryHql("select new map(id as id, expertName as expert_name, number as number, title as title, deptName as dept_name, branchName as branch_name) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId());
		if (list.size() == 0) {
			return new Message(30090, "暂无数据");
		}
		String _voterIds = "-1";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String voterId = Tools.getDesc(map.get("id"), "");
			if ("".equals(voterId)) 
				continue;
			_voterIds += "," + voterId;
		}
		List counts = QBuilder.db("basedb").queryHql("select new map(voterId as id, count(id) as count) from VoteInfo where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voterId in (" + _voterIds + ") group by voterId"); 
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			map.put("count", 0);
			String voterId = Tools.getDesc(map.get("id"), "");
			for (int j = 0; j < counts.size(); j++) {
				Map temp = (Map)counts.get(j);
				String id = Tools.getDesc(temp.get("id"), "-1");
				if (voterId.equals(id)) {
					map.put("count", Tools.getInt(temp.get("count") + "", 0));
					break;
				}
			}
		}
		// 按票数进行排序
		Collections.sort(list, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				return Tools.getInt(((Map)o2).get("count") + "", 0) - Tools.getInt(((Map)o1).get("count") + "", 0);
			}
		});
		msg.put("voters", list);
		List<Integer> countSort = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			int count = Tools.getInt(map.get("count") + "", 0);
			if (!countSort.contains(count)) {
				countSort.add(count);
			}
		}
		Collections.sort(countSort);
		Collections.reverse(countSort);
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			int count = Tools.getInt(map.get("count") + "", 0);
			for (int j = 0; j < countSort.size(); j++) {
				if (j < 3 && countSort.get(j) == count) {
					map.put("sort", "第" + (j + 1) + "名");
					break;
				}
			}
		}
		return msg;
	}
	
	// 通过编号查询
	public Message queryById(Voter voter, VoteInfo vi) {
		Message msg = new Message();
		List list = QBuilder.db("basedb").queryHql("select new map(id as id, expertName as expert_name, number as number, avatar as avatar, title as title, deptName as dept_name, branchName as branch_name) from Voter where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voteId = " + voter.getVoteId() + " and number = '" + voter.getNumber() + "'");
		if (list.size() == 0) {
			return new Message(30090, "暂无数据");
		}
		Map map = (Map)list.get(0);
		String voterId = Tools.getDesc(map.get("id"), "");
		List<Integer> voterIds = QBuilder.db("basedb").queryHql("select voterId from VoteInfo where hospital = " + vi.getHospital() + " and status = " + STATUS_ENABLE + " and uid = '" + vi.getUid() + "' and date = '" + vi.getDate() + "' and cancel = " + VOTE_ENABLE);
		map.put("state", voterIds.contains(Tools.getDesc(voterId, 0)));
		List<Integer> counts = QBuilder.db("basedb").queryHql("select count(id) from VoteInfo where hospital = " + voter.getHospital() + " and status = " + STATUS_ENABLE + " and voterId = " + voterId); 
		map.put("count", counts.get(0));
		list = new ArrayList();
		list.add(map);
		msg.put("voters", list);
		return msg;
	}
%>